﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Arcanea.Mediator.XmlClasses;
using System.IO;
using Arcanea.Mediator.Toolbox;
using System.Diagnostics;

namespace Arcanea.PacketEditor
{
    public partial class UserControlPacketEditor : UserControl
    {
        private IList<XmlPacket> AllXmlPackets = null;

        public static string xmlFileNamePacket = "Packets.xml";
        public static string XMLFileNamePacket
        {
            get { return xmlFileNamePacket; }
            set { xmlFileNamePacket = value; }
        }

        public static string xmlFilePathPacket = Path.Combine(Application.StartupPath, XMLFileNamePacket);
        public static string XMLFilePathPacket
        {
            get { return xmlFilePathPacket; }
            set { xmlFilePathPacket = value; }
        }

        public UserControlPacketEditor()
        {
            InitializeComponent();
        }


        #region Main window events

        private void MainForm_Load(object sender, EventArgs e)
        {
            if (Process.GetCurrentProcess().ProcessName != "devenv")
            {
                AllXmlPackets = XMLToolbox.XmlListPacketLoading(xmlFilePathPacket);
                UpdateControls();
                timerSave.Enabled = true;
            }
        }

        public void SaveState(object sender, FormClosingEventArgs e)
        {
            XMLToolbox.XmlListPacketSaving(xmlFilePathPacket, AllXmlPackets);
        }

        #endregion

        #region MainForm synchronize

        public void UpdateControls()
        {
            listViewPacket.Items.Clear();
            foreach (XmlPacket p in AllXmlPackets)
            {
                listViewPacket.Items.Add(p.ToString(), 2);
            }

            listBoxField_SelectedIndexChanged(null, null);
            listViewPacket_SelectedIndexChanged(null, null);
        }

        public void UpdateFieldsControl()
        {
            if (listViewPacket.SelectedIndices.Count == 1)
            {
                listBoxField.Items.Clear();
                XmlPacket p = AllXmlPackets[listViewPacket.SelectedIndices[0]];
                IList<XmlField> lf = p.Fields;
                if (p.Fields != null)
                    foreach (XmlField f in lf)
                    {
                        listBoxField.Items.Add(f);
                    }
            }
        }

        #endregion

        private void listViewPacket_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Met a jour le menu strip
            if (listViewPacket.SelectedIndices.Count == 0)
            {
                modifierToolStripMenuItem.Visible = false;
                supprimerToolStripMenuItem.Visible = false;
                ajouterToolStripMenuItem.Visible = true;

                modifierToolStripMenuItemField.Visible = false;
                supprimerToolStripMenuItemField.Visible = false;
                ajouterToolStripMenuItemField.Visible = false;
                labelCommentsPacket.Text = String.Empty;
                labelCommentsField.Text = String.Empty;
                listBoxField.Items.Clear();
                listBoxField.Enabled = false;
            }
            else
            {
                modifierToolStripMenuItem.Visible = true;
                supprimerToolStripMenuItem.Visible = true;
                ajouterToolStripMenuItem.Visible = false;

                modifierToolStripMenuItemField.Visible = false;
                supprimerToolStripMenuItemField.Visible = false;
                ajouterToolStripMenuItemField.Visible = true;
                labelCommentsPacket.Text = AllXmlPackets[listViewPacket.SelectedIndices[0]].Description;
                listBoxField.Enabled = true;
                UpdateFieldsControl();
            }
        }

        #region MenuStrip Packet

        private void ajouterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PacketForm pf = new PacketForm();
            DialogResult dr = pf.ShowDialog(this);
            if (dr == DialogResult.OK)
            {
                AllXmlPackets.Add(pf.Paquet);
            }
            UpdateControls();
        }

        private void modifierToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listViewPacket.SelectedIndices.Count == 1)
            {
                PacketForm pf = new PacketForm();
                pf.Paquet = AllXmlPackets[listViewPacket.SelectedIndices[0]];
                DialogResult dr = pf.ShowDialog(this);
                if (dr == DialogResult.OK)
                {
                    AllXmlPackets[listViewPacket.SelectedIndices[0]] = pf.Paquet;
                }
                int index = listViewPacket.SelectedIndices[0];
                UpdateControls();
                listViewPacket.SelectedIndices.Add(index);
            }
        }

        private void supprimerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listViewPacket.SelectedIndices.Count == 1)
            {
                AllXmlPackets.RemoveAt(listViewPacket.SelectedIndices[0]);
                UpdateControls();
            }
        }


        #endregion

        private void timerSave_Tick(object sender, EventArgs e)
        {
            XMLToolbox.XmlListPacketSaving(xmlFilePathPacket, AllXmlPackets);
            toolStripStatusLabel1.Text = "Saved on " + DateTime.Now.ToString("T");
        }

        private void listBoxField_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Met a jour le menu strip
            if (listBoxField.SelectedIndex == -1)
            {
                modifierToolStripMenuItemField.Visible = false;
                supprimerToolStripMenuItemField.Visible = false;

                buttonMonter.Enabled = false;
                buttonDescendre.Enabled = false;

                labelCommentsField.Text = String.Empty;

            }
            else
            {
                modifierToolStripMenuItemField.Visible = true;
                supprimerToolStripMenuItemField.Visible = true;

                buttonMonter.Enabled = true;
                buttonDescendre.Enabled = true;
                if (listViewPacket.SelectedIndices.Count != 0)
                    labelCommentsField.Text = AllXmlPackets[listViewPacket.SelectedIndices[0]].Fields[listBoxField.SelectedIndex].Description;

            }
        }

        #region MenuStrip Field

        private void ajouterToolStripMenuItemField_Click(object sender, EventArgs e)
        {
            if (listViewPacket.SelectedIndices.Count == 1)
            {
                FieldForm ff = new FieldForm();
                DialogResult dr = ff.ShowDialog(this);
                if (AllXmlPackets[listViewPacket.SelectedIndices[0]].Fields == null)
                    AllXmlPackets[listViewPacket.SelectedIndices[0]].Fields = new List<XmlField>();

                ff.AllXmlPackets = AllXmlPackets;

                if (dr == DialogResult.OK)
                {
                    AllXmlPackets[listViewPacket.SelectedIndices[0]].Fields.Add(ff.Field);
                }
                UpdateFieldsControl();
            }
        }

        private void modifierToolStripMenuItemField_Click(object sender, EventArgs e)
        {
            if (listViewPacket.SelectedIndices.Count == 1 && listBoxField.SelectedIndex != -1)
            {
                FieldForm ff = new FieldForm();
                ff.Field = AllXmlPackets[listViewPacket.SelectedIndices[0]].Fields[listBoxField.SelectedIndex];
                DialogResult dr = ff.ShowDialog(this);
                if (dr == DialogResult.OK)
                {
                    AllXmlPackets[listViewPacket.SelectedIndices[0]].Fields[listBoxField.SelectedIndex] = ff.Field;
                }
                UpdateFieldsControl();
            }
        }

        private void supprimerToolStripMenuItemField_Click(object sender, EventArgs e)
        {
            if (listViewPacket.SelectedIndices.Count == 1)
            {
                AllXmlPackets[listViewPacket.SelectedIndices[0]].Fields.RemoveAt(listBoxField.SelectedIndex);
                UpdateFieldsControl();
                listBoxField_SelectedIndexChanged(sender, e);
            }
        }


        #endregion

        #region Gestion du double click

        private void listViewPacket_DoubleClick(object sender, EventArgs e)
        {
            if (listViewPacket.SelectedIndices.Count == 0)
            {
                //On ajoute
                ajouterToolStripMenuItem_Click(sender, e);
            }
            else
            {
                //On modifie
                modifierToolStripMenuItem_Click(sender, e);
            }
        }

        private void listBoxField_DoubleClick(object sender, EventArgs e)
        {
            if (listBoxField.SelectedIndex == -1)
            {
                //On ajoute
                ajouterToolStripMenuItemField_Click(sender, e);
            }
            else
            {
                //On modifie
                modifierToolStripMenuItemField_Click(sender, e);
            }
        }

        #endregion

        #region gestion de la réorganisation des fields

        private void buttonMonter_Click(object sender, EventArgs e)
        {
            if (listBoxField.SelectedIndex != -1 && listBoxField.SelectedIndex != 0)
            {
                XmlField temp = AllXmlPackets[listViewPacket.SelectedIndices[0]].Fields[listBoxField.SelectedIndex - 1];
                AllXmlPackets[listViewPacket.SelectedIndices[0]].Fields[listBoxField.SelectedIndex - 1] =
                    AllXmlPackets[listViewPacket.SelectedIndices[0]].Fields[listBoxField.SelectedIndex];
                AllXmlPackets[listViewPacket.SelectedIndices[0]].Fields[listBoxField.SelectedIndex] = temp;

                int indexListView = listViewPacket.SelectedIndices[0];
                int indexListBox = listBoxField.SelectedIndex;

                UpdateControls();
                listViewPacket.SelectedIndices.Add(indexListView);
                listBoxField.SelectedIndex = indexListBox - 1;
            }
        }

        private void buttonDescendre_Click(object sender, EventArgs e)
        {
            if (listBoxField.SelectedIndex != -1 && listBoxField.SelectedIndex != listBoxField.Items.Count - 1)
            {

                XmlField temp = AllXmlPackets[listViewPacket.SelectedIndices[0]].Fields[listBoxField.SelectedIndex + 1];
                AllXmlPackets[listViewPacket.SelectedIndices[0]].Fields[listBoxField.SelectedIndex + 1] =
                    AllXmlPackets[listViewPacket.SelectedIndices[0]].Fields[listBoxField.SelectedIndex];
                AllXmlPackets[listViewPacket.SelectedIndices[0]].Fields[listBoxField.SelectedIndex] = temp;

                int indexListView = listViewPacket.SelectedIndices[0];
                int indexListBox = listBoxField.SelectedIndex;

                UpdateControls();
                listViewPacket.SelectedIndices.Add(indexListView);
                listBoxField.SelectedIndex = indexListBox + 1;
            }
        }

        #endregion

    }
}
